#!/bin/bash

set -e

dataMnt=/mnt/server.lan/data
dataStorage=$dataMnt/dataStorage

flag=false
if [ ! -e "$dataStorage" ];then
  echo "[E] $dataStorage : dir not exists,please make it"
  flag=true
fi

if [ -z "$TZ" ];then
  # "Asia/Shanghai"
  echo '[ERR] env TZ  not set, and install "tzdata" '
  flag=true
fi

if $flag;then
  exit 1
fi

###################
## settings
name="seafile.server.lan"
image="inthbl/seafile-8.0.7"

docker pull "${image}"

mkDir() {
  if [ -z "$1" ]; then
    return
  fi

  if [ ! -e "${1}" ]; then
    mkdir -p "${1}"
    chown 1000:1000 "${1}"
  fi
}
dataDir="$dataStorage/$name"
mkDir "${dataDir}"

docker run -d --name ${name} \
  --restart=always \
  -v ${dataDir}:/shared \
  -p 127.0.0.1:8000:8000 \
  -p 127.0.0.1:8080:8080 \
  -p 127.0.0.1:8082:8082 \
  -e TZ=$TZ \
  -e SQLITE=1 \
  -e SERVER_IP=seafile.server.lan \
  -e USE_HTTPS=1 \
  -e SEAFILE_ADMIN_EMAIL=admin@admin.lan \
  -e SEAFILE_ADMIN_PASSWORD=admin \
  $image

exit $?
